package com.zt.bus.sms.service;

import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.AlibabaAliqinFcSmsNumSendRequest;
import com.taobao.api.response.AlibabaAliqinFcSmsNumSendResponse;
import com.zt.bus.sms.model.SmsLog;
import com.zt.constant.Constants;
import com.zt.util.PropertiesUtil;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * Created by Administrator on 2017/7/5.
 */
@Service
public class SendCodeService {

    @Autowired
    private SmsLogService smsLogService;

    private static final Logger LOG = Logger.getLogger(SendCodeService.class);

    // 阿里大鱼
    public static final String URL = PropertiesUtil.getProperty("url");
    public static final String APP_KEY = PropertiesUtil.getProperty("appkey");
    public static final String SECRET = PropertiesUtil.getProperty("secret");

    public boolean sendSms(String businessId, String phone, String msgParam, String content, String code) {
        boolean result = false;
        try {

            LOG.info("发送请求开始：" + phone + "|" + msgParam + "|" + content + "|" + code);

            TaobaoClient client = new DefaultTaobaoClient(URL, APP_KEY, SECRET);
            AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();

            req.setSmsType("normal");
            req.setSmsParamString(msgParam);
            req.setSmsFreeSignName("定制公交");
            req.setRecNum(phone);
            req.setSmsTemplateCode(code);
            AlibabaAliqinFcSmsNumSendResponse rsp = client.execute(req);

            LOG.info("请求结果返回：" + rsp.getBody());
            // System.out.println(rsp.getBody());

            JSONObject retJson = JSONObject.fromObject(rsp.getBody());
            if (retJson.getJSONObject("alibaba_aliqin_fc_sms_num_send_response").getJSONObject("result")
                    .get("err_code").equals("0")) {
                SmsLog smsLog = new SmsLog();
                smsLog.setContent(content);
                smsLog.setSendTime(new Date());
                smsLog.setPhone(phone);
                smsLog.setSmsResult(Constants.Status.ENABLE);
                smsLog.setBusinessId(businessId);
                smsLogService.insert(smsLog);
                result = true;
            } else {
                SmsLog smsLog = new SmsLog();
                smsLog.setContent(content);
                smsLog.setSendTime(new Date());
                smsLog.setPhone(phone);
                smsLog.setSmsResult(Constants.Status.DISABLE);
                smsLog.setBusinessId(businessId);
                smsLogService.insert(smsLog);
                result = false;
            }
        } catch (Exception e) {
            SmsLog smsLog = new SmsLog();
            smsLog.setContent(content);
            smsLog.setSendTime(new Date());
            smsLog.setPhone(phone);
            smsLog.setSmsResult(Constants.Status.DISABLE);
            smsLog.setBusinessId(businessId);
            smsLogService.insert(smsLog);
            result = false;
            e.printStackTrace();
        }
        return result;
    }
}
